package com.lbs.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbs.entity.CourseUser;
import com.lbs.service.CourseUserService;
import com.lbs.service.Impl.CourserUserServiceImpl;
import com.lbs.utils.BaseServlet;
import com.lbs.utils.PageUtils;
import com.lbs.utils.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2021/6/2 10:13
 * @description
 **/
@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {

    CourseUserService courseUserService = new CourserUserServiceImpl();
    ResultVO resultVO = null;
    ObjectMapper mapper = new ObjectMapper();

    /**
     * 分页查询选课信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageCourseUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search = request.getParameter("search");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        PageUtils<CourseUser> pageUtils = courseUserService.pageCourseUser(search,pageSize,currentPage);
        if (pageUtils!=null){
            resultVO = new ResultVO(200,"查询选课信息成功！",pageUtils);
        }else {
            resultVO = new ResultVO(200,"查询选课信息失败！",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 修改选课信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void updateCourseUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cid = Integer.parseInt(request.getParameter("cid"));
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = courseUserService.updateCourseUser(cid,id);
        if (flag){
            resultVO = new ResultVO(200,"修改成功！",null);
        }else {
            resultVO = new ResultVO(500,"修改失败！",null);
        }
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 批量删除选课信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void batchDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("id");
        boolean flag = courseUserService.batchDel(ids);
        if (flag){
            resultVO = new ResultVO(200,"删除成功！",null);
        }else {
            resultVO = new ResultVO(200,"删除失败！",null);
        }
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }
}
